/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tutwiz.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.tutwiz.util.DatabasePopulator;

/**
 *
 * @author danielwirdehall
 */
public class FrontController {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public FrontController(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		// Parsar ut url'en ut efter "nice urls"-pattern
        URL theUrl = new URL(request.getRequestURL().toString());
        int position = theUrl.getPath().lastIndexOf("tutwiz");
        String thePath;
        if (position == -1) {
            thePath = theUrl.getPath().substring(1);
        } else {
            thePath = theUrl.getPath().substring(position + 7);
        }
        String[] params = thePath.split("/");
		
		// Om vi inte får en controller angiven så ska vi gå till index.php
        if (params[0].equals("")) {
            request.getRequestDispatcher("/index.xhtml").forward(request, response);
        } else if (params[0].equals("populateDatabase")) { // en temp metod för att pupulera database.
            DatabasePopulator.populate();
        } else {
			// Anropar den angivna controllen, om controllen inte finns anropa index.xhtml
            Controller controller = null;
            if (params[0].equals("user")) {
                controller = new UserController(params, request, response);
            } else if (params[0].equals("tutorial")) {
                controller = new TutorialController(params, request, response);
            } else if (params[0].equals("tag")) {
                controller = new TagController(params, request, response);
            } else if (params[0].equals("comment")) {
                controller = new CommentController(params, request, response);
            } else if (params[0].equals("step")) {
                controller = new StepController(params, request, response);
            } else if (params[0].equals("vote")) {
                controller = new VoteController(params, request, response);
            } else {
				request.getRequestDispatcher("/index.xhtml").forward(request, response);
			}
			// Anropa funktionen som parasar ut vilken action som du ville köra för angiven controller engligt restuful-pattern.
			if (controller != null) {
	            controller.doAction();
			}
        }
    }
}
